/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.entity;

import br.com.fatecpg.songs.entity.util.Searchable;
import br.com.fatecpg.songs.entity.util.Securable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.Where;

/**
 *
 * @author Marcos
 */
@SequenceGenerator(name="SEQARTIST", sequenceName="SEQARTIST", allocationSize=1, initialValue=1)
@Entity(name="Artist")
@Table(name="TB_ARTIST")
public class Artist implements Securable, Searchable{
    
    public Artist(){
        musicians = new ArrayList<Musician>();        
        originCity = new City();      
        genre = new Genre();
    }
    
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="SEQARTIST")
    @Column(name="id")
    private Long id;
    
    @Column(name = "name")
    private String name;
    
    @Column(name = "artist_year")
    private int year;
    
    @Column(name = "history")
    private String history;
    
    @ManyToOne    
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinColumn(name = "id_origin_city", referencedColumnName = "id")
    private City originCity;
        
    @ManyToOne   
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinColumn(name = "id_genre", referencedColumnName = "id")
    private Genre genre;
    
    @LazyCollection(LazyCollectionOption.FALSE)
    @ManyToMany          
    @JoinTable(name = "TB_ARTIST_MUSICIAN", 
        joinColumns = {@JoinColumn(name = "id_artist", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "id_musician", referencedColumnName = "id")})
    private List<Musician> musicians;
    
    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(mappedBy = "artist")
    @JoinColumn(name ="id_artist", referencedColumnName = "id")               
    private Set<Album> albums;
    
    @ManyToOne
    @JoinColumn(name = "id_updater", referencedColumnName = "id")
    private User updaterUser;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_update")
    private Date lastUpdate;

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getHistory() {
        return history;
    }

    public void setHistory(String history) {
        this.history = history;
    }

    public City getOriginCity() {
        return originCity;
    }

    public void setOriginCity(City originCity) {
        this.originCity = originCity;
    }

    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    public List<Musician> getMusicians() {
        return musicians    ;
    }

    public void setMusicians(List<Musician> musicians) {
        this.musicians = musicians;
    }

    public Set<Album> getAlbums() {
        return albums;
    }
    
    public List<Album> getAlbumsAsList() {
        return new LinkedList<Album>(albums);
    }    
    
    public void setAlbums(Set<Album> albums) {
        this.albums = albums;
    }

    @Override
    public User getUpdaterUser() {
        return updaterUser;
    }

    @Override
    public void setUpdaterUser(User updaterUser) {
        this.updaterUser = updaterUser;
    }

    @Override
    public Date getLastUpdate() {
        return lastUpdate;
    }

    @Override
    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }        

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Artist other = (Artist) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }        
}
